﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Breed_养殖_
{
    public class GetHealthcareHandler : IRequestHandler<GetHealthcareCommand, ApiPaging<HealthcareDTO>>
    {
        private readonly IBase<HealthcareModel> healthcareRepo;

        public GetHealthcareHandler(IBase<HealthcareModel> healthcareRepo)
        {
            this.healthcareRepo = healthcareRepo;
        }
        public Task<ApiPaging<HealthcareDTO>> Handle(GetHealthcareCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<HealthcareDTO> result = new ApiPaging<HealthcareDTO>();
            var healthcare = healthcareRepo.GetAll().Where(x => !x.IsDeleted);
            if(!string.IsNullOrEmpty(request.Batch))
            {
                healthcare = healthcare.Where(x => x.Batch.Contains(request.Batch));
            }
            var linq = from x in healthcare
                        select new HealthcareDTO
                        {
                            Batch = x.Batch,
                            HealthcareId = x.HealthcareId,
                            HealthcareMethod = x.HealthcareMethod,
                            KeepDays = x.KeepDays,
                            LnjectMethod = x.LnjectMethod,
                        };
            result.Data = linq.ToList();
            result.Code = ApiEnum.OK;
            result.Msg = "养殖保健信息显示查询成功";
            return Task.FromResult(result);
        }
    }
}
